package com.gitee.blogcase.dealyqueuecase.controller;

import com.gitee.blogcase.dealyqueuecase.java.JavaDelayQueueConsumer;
import com.gitee.blogcase.dealyqueuecase.java.OrderTimeoutDelayed;
import com.gitee.blogcase.dealyqueuecase.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * JUC实现延迟队列
 * @author wuwentao
 */
@Slf4j
@RestController
@RequestMapping("java/dealy/queue")
@AllArgsConstructor
public class JavaDealyQueueProducer {
    private JavaDelayQueueConsumer javaDelayQueueConsumer;
    private final int defaultTimoutSecond = 10; // 过期秒数
    /**
     * 发送消息到JUC延迟队列中
     * http://localhost:8022/java/dealy/queue/sendMessage?message=100000001
     * http://localhost:8022/java/dealy/queue/sendMessage?message=100000002
     * http://localhost:8022/java/dealy/queue/sendMessage?message=100000003
     * http://localhost:8022/java/dealy/queue/sendMessage?message=100000004
     * http://localhost:8022/java/dealy/queue/sendMessage?message=100000005
     * @param message 消息内容
     */
    @GetMapping("/sendMessage")
    public String sendMessage(@RequestParam(value = "message") String message){
        // 监听订单10秒后过期
        javaDelayQueueConsumer.monitor(new OrderTimeoutDelayed(message,defaultTimoutSecond));
        log.info("当前时间为:[{}] 订单号为:[{}] 超时秒数:[{}]", DateUtil.getCuurentDateStr(),message,defaultTimoutSecond);
        return "OK";
    }
}
